import { createApp } from 'vue';
import './style.css';
import 'virtual:uno.css';
import App from './App.vue';
import router from './router';
import { globalRegisterComponents } from './components';
import { globalRegisterDirectives } from './directives';
import { guard } from './router/guards';
import { loadingRouter } from './router/routeInit';
import { createPinia } from 'pinia';
import { enableMocking } from './mocks/msw/browser';
import 'animate.css'
// import VxeUITable from 'vxe-table'
// import 'vxe-table/lib/style.css'
// import VxeUIAll from 'vxe-pc-ui'
// import 'vxe-pc-ui/lib/style.css'
const app = createApp(App);
const pinia = createPinia();

async function globalInit() {
  // 开启mocks
  // if (import.meta.env.MODE === 'development') {
  //   try {
  //     await enableMocking();
  //   } catch (error) {
  //     console.error('MSW setup failed:', error);
  //   }
  // }
  // 注册指令
  globalRegisterDirectives(app);

  // 注册全局组件
  await globalRegisterComponents(app);

  /**
   * 首次进入页面 如果已经有登录 在挂载路由之前加载动态路由，就不会警告找不到路由了 [Vue Router warn]: No match found
   * 即便警告也没关系 也会照常加载动态路由
   * */
  const token = localStorage.getItem('token');
  if (token) {
    await loadingRouter();
    console.log('已登录');
  }

  // 挂载路由
  app.use(router);
  app.use(pinia);

  // app.use(VxeUITable);
  // app.use(VxeUIAll);

  // 开启路由拦截
  guard();

  // 挂载应用
  app.mount('#app');
}

globalInit();
